# -*- coding: utf-8 -*-
# @Time : 2022/1/10 11:13

from Common.mysql import db
from src.User.Database.user_database import User
from Common.public_method import md_5, utc_8
from sqlalchemy.exc import SQLAlchemyError
from Common.yaml_method import YamlMethod
from sqlalchemy import or_


class UpdateUser:
    """
    更新用户信息接口
    """

    @staticmethod
    def update_user(user_id, username, password, real_name, roles, status, projects, update_user):
        """
        更新用户信息接口
        :param user_id: 用户ID
        :param username: 登录账号
        :param password: 登录密码
        :param real_name: 真实姓名
        :param roles: 角色
        :param status: 账号状态
        :param projects: 所属项目
        :param update_user: 更新人
        :return:
        """

        code = YamlMethod().read_data('code.yaml')['code']

        update_time = utc_8()

        # 判断是否存在相同登录账号名称和真实姓名
        name_info = User.query.filter(or_(User.userName == username, User.realName == real_name)).all()
        if len(name_info) <= 1:
            # 查询用户信息
            user = User.query.filter_by(id=user_id).first()
            if user:
                user.username = username
                user.realName = real_name
                user.roles = roles
                user.status = status
                user.user_projects = projects
                user.update_time = update_time
                user.update_user = update_user

                if password:
                    user.password = md_5(password)
                try:
                    db.session.commit()
                except SQLAlchemyError:
                    db.session.rollback()
                    res = {
                        'code': code[6],
                        'data': [],
                        'message': '更新用户信息失败'
                        }
                    return res

                res = {
                    'code': code[0],
                    'data': [],
                    'message': '更新用户信息成功'
                    }
                return res
            else:
                res = {
                    'code': code[3],
                    'data': [],
                    'message': '用户不存在'
                }
                return res
        else:
            res = {
                'code': code[1],
                'message': '登录账号名称已存在',
                'data': {}
            }
            return res

    @staticmethod
    def update_user_project(real_name, bug_project):
        """
        更新用户信息接口
        :param real_name: 真实姓名
        :param bug_project: 最近访问的缺陷管理的项目ID
        :return:
        """

        code = YamlMethod().read_data('code.yaml')['code']

        user = User.query.filter_by(realName=real_name).first()

        if user:
            user.bug_project = bug_project
            try:
                db.session.commit()
            except SQLAlchemyError:
                db.session.rollback()

            res = {
                'code': code[0],
                'data': [],
                'message': '更新用户信息成功'
            }
            return res
        else:
            res = {
                'code': code[3],
                'data': [],
                'message': '用户不存在'
            }
            return res

    @staticmethod
    def update_user_password(user_name, password):
        """
        更新用户信息接口
        :param user_name:
        :param password: 登录密码
        :return:
        """
        code = YamlMethod().read_data('code.yaml')['code']

        user = User.query.filter_by(userName=user_name).first()

        if user:
            user.password = md_5(password)
            try:
                db.session.commit()
            except SQLAlchemyError:
                db.session.rollback()

            res = {
                'code': code[0],
                'data': [],
                'message': '更新密码成功'
            }
            return res
        else:
            res = {
                'code': code[3],
                'data': [],
                'message': '用户不存在'
            }
            return res
